//
//  UUSupplierPeakSeasonPicker.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 27/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUSupplierPeakSeasonPicker.h"

@interface UUSupplierPeakSeasonPicker ()

@property (nonatomic, strong) NSMutableOrderedSet *selectedPeakSeasonIds;

@property (nonatomic, strong) UIButton *bottomView;

@end

@implementation UUSupplierPeakSeasonPicker

static UIViewController *_picker;
+ (UIViewController *)pickerWithSelectedPeakSeasonIds:(NSArray<NSNumber *> *)selectedPeakSeasonIds
                                        completeBlock:(void (^)(NSArray<NSNumber *> *peakSeasonIds))completeBlock {
    UUSupplierPeakSeasonPicker *picker = [self instanceOfAppropriate];
    picker.title = @"请选择旺季";
    picker.selectedPeakSeasonIds = [NSMutableOrderedSet orderedSetWithArray:selectedPeakSeasonIds];
    WEAK_SELF();
    picker.didSelectPeakSeasonBlock = ^(NSArray<NSNumber *> *peakSeasonIds) {
        completeBlock(peakSeasonIds);
        [weakSelf dismiss];
    };
    picker.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"取消"
                                                                               target:self
                                                                             selector:@selector(dismiss)];
    _picker = picker;
    return [[UINavigationController alloc] initWithRootViewController:picker];
}

+ (void)dismiss {
    [_picker dismissViewControllerAnimated:YES completion:nil];
    _picker = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.allowsMultipleSelection = YES;
    
    self.bottomView = [UIButton buttonWithType:UIButtonTypeCustom];
    self.bottomView.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.bottomView setBackgroundColor:[UIColor whiteColor]];
    [self.bottomView setTitleColor:COMMON_TEXT_ORANGE_COLOR forState:UIControlStateNormal];
    [self.bottomView addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
    
    [self updateBottomViewStatus];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateBottomViewStatus {
    if (self.selectedPeakSeasonIds.count > 0) {
        [self.bottomView setTitle:[NSString stringWithFormat:@"确定(%ld个)", self.selectedPeakSeasonIds.count]
                         forState:UIControlStateNormal];
        [self.view addSubview:self.bottomView];
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
        }];
        [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(44);
            make.top.equalTo(self.tableView.mas_bottom);
        }];
    } else {
        [self.bottomView removeFromSuperview];
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
}

- (void)done:(id)sender {
    self.didSelectPeakSeasonBlock([self.selectedPeakSeasonIds array]);
}

- (void)configCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSNumber *peakSeasonId = [self peakSeasonIdForIndexPath:indexPath];
    if ([self.selectedPeakSeasonIds containsObject:peakSeasonId]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        NSNumber *peakSeasonId = [self peakSeasonIdForIndexPath:indexPath];
        [self.selectedPeakSeasonIds addObject:peakSeasonId];
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        NSNumber *peakSeasonId = [self peakSeasonIdForIndexPath:indexPath];
        [self.selectedPeakSeasonIds removeObject:peakSeasonId];
    }
    
    [self updateBottomViewStatus];
}

@end
